package com.wechat.wechat.controller;

import com.wechat.wechat.converter.OrderForm2OrderDTOConverter;
import com.wechat.wechat.dto.OrderDTO;
import com.wechat.wechat.enums.ResultEnum;
import com.wechat.wechat.exception.SellException;
import com.wechat.wechat.form.OrderForm;
import com.wechat.wechat.service.BuyerService;
import com.wechat.wechat.service.OrderService;
import com.wechat.wechat.utils.ResultVOUtil;
import com.wechat.wechat.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author NN
 * @2020/2/21 15:08
 */
@RestController
@RequestMapping("/buyer/order")
@Slf4j
public class BuyerOrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private BuyerService buyerService;

    /**
     * 创建订单 POST /sell/buyer/order/create
     */
    @PostMapping("/create")
    public ResultVO<Map<String,String>> create(@Valid OrderForm orderForm,
                                               BindingResult bindingResult)
    {
        if(bindingResult.hasErrors())
        {
            log.error("【创建订单】 参数不正确,orderForm={}",orderForm.toString());
            throw new SellException(ResultEnum.PARAM_ERROR.getCode(),
                    bindingResult.getFieldError().getDefaultMessage());
        }
        OrderDTO orderDTO= OrderForm2OrderDTOConverter.convert(orderForm);
        if(CollectionUtils.isEmpty(orderDTO.getOrderDetailList()))
        {
            log.error("【创建订单】 购物车不能为空");
            throw new SellException(ResultEnum.CART_EMPTY);
        }
        OrderDTO result=orderService.create(orderDTO);
        Map<String,String> map = new HashMap<>();
        map.put("orderId",orderDTO.getOrderId());
        return ResultVOUtil.success(map);
    }
    /**
    * 分页获取订单列表 GET /sell/buyer/order/list
    */
    @GetMapping("/list")
    public ResultVO<List<OrderDTO>> list(@RequestParam("openid") String openid,
                                         @RequestParam(value="page",defaultValue="0") Integer page,
                                         @RequestParam(value="size",defaultValue="10") Integer size)
    {
        if(StringUtils.isEmpty(openid))
        {
            log.error("【查询订单列表】openid为空");
            throw new SellException(ResultEnum.PARAM_ERROR);
        }
        PageRequest request=new PageRequest(page,size);
        Page<OrderDTO> orderDTOPage = orderService.findList(openid,request);
        return ResultVOUtil.success(orderDTOPage.getContent());
    }

    /**
     * 查询订单详情 GET /sell/buyer/order/detail
     */
    @GetMapping("/detail")
    public ResultVO<OrderDTO> detail(@RequestParam("openid") String openid,
                                     @RequestParam("orderId") String orderId)
    {
        OrderDTO orderDTO=buyerService.findOrderOne(openid,orderId);
        return ResultVOUtil.success(orderDTO);
    }
    /**
     * 取消订单 POST /sell/buyer/order/cancel
     */
    @PostMapping("/cancel")
    public ResultVO cancel(@RequestParam("openid") String openid,
                           @RequestParam("orderId") String orderId)
    {
        OrderDTO orderDTO=buyerService.cancelOrder(openid,orderId);
        return ResultVOUtil.success(null);
    }


}
